package com.iyundao.io.Service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iyundao.io.Service.IntegralService;
import com.iyundao.io.Service.WishService;
import com.iyundao.io.mapper.*;
import com.iyundao.io.model.*;
import com.iyundao.io.tools.GiftVo;
import com.iyundao.io.tools.IntegralOperationVo;
import com.iyundao.io.tools.ResponseData;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class WishServiceImpl implements WishService {

    private static Integer count = 0001;

    @Autowired
    private WishMapper wishMapper;

    @Autowired
    private WishSubmitMapper wishSubmitMapper;

    @Autowired
    private WishSubmitDetailMapper wishSubmitDetailMapper;

    @Autowired
    private GiftMapper giftMapper;

    @Autowired
    private IntegralService integralService;

    @Autowired
    private WishCustomMapper wishCustomMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private PartyMemberCustomMapper memberCustomMapper;

    @Autowired
    private SysDeptCustomMapper deptCustomMapper;

    @Autowired
    private IntegralMapper integralMapper;

    @Override
    public ResponseData insert(Wish wish) {

        if (wish.equals("") || wish == null) {
            return new ResponseData(0, "传入数据为空", null, null);
        } else {
            int i1 = wishMapper.selectExist(wish);
            if (i1 > 0) {
                return new ResponseData(0,"该礼品已加入心愿单");
            } else {
                int i = wishMapper.insert(wish);
                if (i > 0) {
                    return new ResponseData(200, "添加成功");
                } else {
                    return new ResponseData(0, "添加失败");
                }
            }

        }

    }

    @Override
    public ResponseData del(Wish wish) {

        if (wish.equals("") || wish == null) {
            return new ResponseData(0, "传入数据为空", null, null);
        } else {
            int i = wishCustomMapper.deleteSelecttive(wish);
            if (i > 0) {
                return new ResponseData(200, "删除成功");
            } else {
                return new ResponseData(0, "删除失败");
            }
        }

    }

    /**
     * 添加兑换提交单
     */
    @Override
    public ResponseData addWishSubmit(List<Wish> list) {
        int i = 0;
        for (Wish wish : list) {
            i += giftMapper.selectById(wish.getGiftid()).getGiftintegral();
        }
        Wish wish = list.get(0);
        WishSubmit wishSubmit = new WishSubmit();
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        wishSubmit.setPartymemberid(wish.getPartymemberid());
        wishSubmit.setSubmitnum("LPTJD" + dateFormat.format(date));
        wishSubmit.setSubmitsumintegral(i);
        wishSubmit.setSubmittime(date);
        int i1 = wishSubmitMapper.insertSelective(wishSubmit);
        if (i1 > 0) {
            List<WishSubmit> list1 = wishSubmitMapper.selectByPartyMemberId(wish.getPartymemberid());
            WishSubmit wishSubmit1 = list1.get(list1.size() - 1);
            Integer id = wishSubmit1.getId();
            return addWishSubmitDetail(list, id);
        } else {
            return new ResponseData(0, "提交失败");
        }

    }

    /**
     * 添加兑换提交单的详细内容
     * @param list     提交的礼品
     * @param submitid 提交单id
     * @return 200，成功，其他失败
     */
    @Override
    public ResponseData addWishSubmitDetail(List<Wish> list, Integer submitid) {
        try {
            for (Wish wish : list) {
                WishSubmitDetail wishSubmitDetail = new WishSubmitDetail();
                wishSubmitDetail.setGiftid(wish.getGiftid());
                wishSubmitDetail.setSubmitid(submitid);
                wishSubmitDetailMapper.insertSelective(wishSubmitDetail);
                del(wish);
            }
            return new ResponseData(200, "提交成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseData(0, e.getMessage());
        }
    }


    /**
     *兑换确认，减去积分，删除提交表数据、提交详情表数据
     * @param integralOperationVo
     *      提交信息：操作人id，提交人id，礼品id，每个礼品的个数
     * @return
     */
    @Override
    public ResponseData exchange(IntegralOperationVo integralOperationVo) {
        List<GiftVo> list = integralOperationVo.getList();
        Integer submitid = integralOperationVo.getSubmitid();
        String userid = integralOperationVo.getUserid();
        Integer partymemberid = integralOperationVo.getPartymemberid();

        try {
            for (GiftVo giftVo: list) {
                IntegralRecord integralRecord = new IntegralRecord();

                //计算所需积分
                Gift gift = giftMapper.selectById(giftVo.getGiftid());
                integralRecord.setIntegralrecordnum(gift.getGiftintegral() * giftVo.getCount());
                integralRecord.setGiftid(giftVo.getGiftid());
                integralRecord.setUserid(userid);
                integralRecord.setPartymemberid(partymemberid);

                //修改积分
                integralService.updateIntegralSum(integralRecord);
            }
            int i = wishSubmitMapper.del(submitid);
            if (i > 0) {
                int i1 = wishSubmitDetailMapper.delete(submitid);
                if (i1 > 0) {
                    return new ResponseData(200,"成功");
                } else {
                    return new ResponseData(0,"删除详情单失败");
                }
            } else {
                return new ResponseData(0,"删除提交单失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseData(0,e.getMessage());
        }

    }

    /**
     * 查询个人的心愿单
     * @param pagenum
     *          页数
     * @param pagesize
     *          每页显示条数
     * @param partymemberid
     *          党员id
     * @return
     */
    @Override
    public PageInfo<Wish> selectWishByPartymemberId(Integer pagenum, Integer pagesize, String partymemberid) {
        List<Wish> list = wishCustomMapper.selectByPartymemberId(partymemberid);
        PageHelper.startPage(pagenum, pagesize);
        PageInfo<Wish> pageInfo = new PageInfo<>(list);
        return  pageInfo;
    }

    /**
     * 查询所有心愿提交数据
     * @param pagenum
     *          页数
     * @param pagesize
     *          每页显示条数
     * @return
     */
    @Override
    public PageInfo<WishSubmit> selectWishSubmitAll(Integer pagenum, Integer pagesize, String sid) {
        List<WishSubmit> list = wishCustomMapper.selectAllBySid(sid);
        PageHelper.startPage(pagenum,pagesize);
        PageInfo<WishSubmit> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    /**
     * 查询每条心愿提交数据的详细数据
     * @param pagenum
     *      页数
     * @param pagesize
     *      每页显示条数
     * @param submitid
     *      提交单的id
     * @return
     */
    @Override
    public ResponseData selectWishSubmitDetailBySubmitId(Integer pagenum, Integer pagesize, Integer submitid) {
        List<Gift> list = wishSubmitDetailMapper.selectBySubmitId(submitid);
        PageHelper.startPage(pagenum,pagesize);
        PageInfo<Gift> pageInfo = new PageInfo<>(list);
        WishSubmit wishSubmit = wishSubmitMapper.selectByid(submitid);

        //个人信息
        SysUser sysUser = sysUserMapper.selectByPrimaryKey(wishSubmit.getPartymemberid());

        PartyMember partyMember = memberCustomMapper.selectAllByPhone(sysUser.getPhone());

        //组织名称
        String s = deptCustomMapper.selectByUid(sysUser.getSid());

        //个人积分
        Integral integral = integralMapper.selectByPartyMemberId(partyMember.getId());
        Map<String, Object> map = new HashMap<>();
        map.put("info",pageInfo);
        map.put("user",sysUser);
        map.put("dept",s);
        map.put("integral",integral);
        return new ResponseData(200,"成功",map);
    }

    /**
     * 根据人员姓名查询提交单
     * @param partymembername
     * @return
     */
    @Override
    public ResponseData selectByLikeName(String partymembername) {
        SysUserProvider sysUser = (SysUserProvider) SecurityUtils.getSubject().getPrincipal();
        String sid = sysUser.getSysDept().getSid();
        String s = "%" + partymembername + "%";
        List<WishSubmitShow> list = wishCustomMapper.selectByLikeName(s, sid);
        return new ResponseData(200,"成功",list);
    }

    /**
     * 根据时间查询提交单
     * @param date
     * @return
     */
    @Override
    public ResponseData selectByDate(String date) {
        SysUserProvider sysUser = (SysUserProvider) SecurityUtils.getSubject().getPrincipal();
        String sid = sysUser.getSysDept().getSid();
        String date1 = date + " 00:00:00";
        String date2 = date + " 23:59:59";
        System.out.println(date);
        List<WishSubmitShow> list = wishCustomMapper.selectByDate(date1, date2, sid);
        return new ResponseData(200,"成功",list);
    }

    /**
     * 查询个人心愿单
     * @param pagenum
     * @param pagesize
     * @return
     */
    @Override
    public PageInfo<GiftShow> selectBySid(Integer pagenum, Integer pagesize) {
        PageHelper.startPage(pagenum,pagesize);
        SysUserProvider sysUser = (SysUserProvider) SecurityUtils.getSubject().getPrincipal();
        String sid = sysUser.getSid();
        List<GiftShow> list = wishCustomMapper.selectBySid(sid);
        PageInfo<GiftShow> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    /**
     * 查看本组织下的心愿提交单
     * @param pagenum
     * @param pagesize
     * @return
     */
    @Override
    public PageInfo<WishSubmitShow> selectByDeptId(Integer pagenum, Integer pagesize) {
        PageHelper.startPage(pagenum,pagesize);
        SysUserProvider sysUser = (SysUserProvider) SecurityUtils.getSubject().getPrincipal();
        String sid = sysUser.getSysDept().getSid();
        List<WishSubmitShow> list = wishCustomMapper.selectByDeptId(sid);
        PageInfo<WishSubmitShow> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public ResponseData deleteWishSubmit(Integer id) {
        try {
            wishSubmitMapper.del(id);
            wishSubmitDetailMapper.delete(id);
            return new ResponseData(200,"删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseData(0,e.getMessage());
        }
    }
}
